﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using GuestBook.DataContext;
using GuestBook.Model;
using System.Web;
using System.Collections;
using System.Web.Configuration;
using System.Collections.Specialized;
using System.Text;

namespace GuestBook.WebApiControllers
{
    public class GuestBookController : ApiController
    {
        private GuestBookDb _db = new GuestBookDb();

        // GET: api/GuestBook
        public IQueryable<GuestBookRecordModel> GetRecords()
        {
            return _db.GuestBookRecords;
        }

        // GET: api/GuestBook/5
        [ResponseType(typeof(GuestBookRecordModel))]
        public IHttpActionResult GetGuestBookRecordModel(int id)
        {
            GuestBookRecordModel guestBookRecordModel = _db.GuestBookRecords.Find(id);
            if (guestBookRecordModel == null)
            {
                return NotFound();
            }

            return Ok(guestBookRecordModel);
        }

        // PUT: api/GuestBook/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutGuestBookRecordModel(int id, GuestBookRecordModel guestBookRecordModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != guestBookRecordModel.Id)
            {
                return BadRequest();
            }

            _db.Entry(guestBookRecordModel).State = EntityState.Modified;

            try
            {
                _db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!GuestBookRecordModelExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/GuestBook
        [ResponseType(typeof(GuestBookRecordModel))]
        public IHttpActionResult PostRecord([FromBody]GuestBookRecordModel guestBookRecordModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            guestBookRecordModel.UserBrowser = GetClientBrowser();
            guestBookRecordModel.UserIP = HttpContext.Current.Request.UserHostAddress;
            guestBookRecordModel.CreatedAt = DateTime.UtcNow;

            _db.GuestBookRecords.Add(guestBookRecordModel);
            _db.SaveChanges();

            //return CreatedAtRoute("DefaultApi", new { id = guestBookRecordModel.Id }, guestBookRecordModel);
            return Ok(guestBookRecordModel);
        }

        private static string GetClientBrowser()
        {
            var userAgent = HttpContext.Current.Request.UserAgent;
            var userBrowser = new HttpBrowserCapabilities { Capabilities = new Hashtable { { string.Empty, userAgent } } };
            var factory = new BrowserCapabilitiesFactory();
            factory.ConfigureBrowserCapabilities(new NameValueCollection(), userBrowser);                        
            return String.Format("{0} {1}", userBrowser.Browser, userBrowser.Version);
        }

        // DELETE: api/GuestBook/5
        [ResponseType(typeof(GuestBookRecordModel))]
        public IHttpActionResult DeleteGuestBookRecordModel(int id)
        {
            GuestBookRecordModel guestBookRecordModel = _db.GuestBookRecords.Find(id);
            if (guestBookRecordModel == null)
            {
                return NotFound();
            }

            _db.GuestBookRecords.Remove(guestBookRecordModel);
            _db.SaveChanges();

            //return Ok(guestBookRecordModel);
            return Ok();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool GuestBookRecordModelExists(int id)
        {
            return _db.GuestBookRecords.Count(e => e.Id == id) > 0;
        }
    }
}